package dao;

import dao.inte.CategoryRepository;
import domain.shop.Category;

import javax.inject.Singleton;
import java.util.List;

@Singleton
public class CategoryRepositoryImpl extends AbstractEntityRepository<Category> implements CategoryRepository {

    public CategoryRepositoryImpl() {
        super(Category.class);
    }

    @Override
    protected Class<Category> getEntityClass() {
        return Category.class;
    }

    public Category create() throws Exception {
        Category category = new Category("New");
        return save(category);
    }

    public Category createChild(Category category) throws Exception {

        Category child = new Category("New child");

        child.setParent(category);

        child = save(child);
        return child;
    }

    public List<Category> findRoots() {
        return entityManager.
                createNamedQuery("Category.findRoots").
                getResultList();
    }

    public List<Category> findChildsByParentId(Long parentId) {
        return entityManager.
                createNamedQuery("Category.findChildsByParentId").setParameter("parentId", parentId).
                getResultList();
    }
}
